package com.qq.spirit;

import java.util.List;
import java.util.Random;

import com.qq.spirit.model.XmlDataUtil;
import com.qq.spirit.model.XmlNodeData;

public class GameUtil
{
	private static Random rd = new Random();

	public static int randomInt(int sed)
	{
		return rd.nextInt(sed);
	}

	public static boolean isGameStateSuc(XmlNodeData xdata)
	{
		if (xdata == null || !xdata.isLoadSuc())
		{
			return false;
		}
		String s = XmlDataUtil.getNodeData(xdata, "results.state");
		if (s.equals("0"))
		{
			return false;
		}
		return true;
	}

	/**
	 * @param o
	 * @return
	 */
	public static int convertInt(Object o)
	{
		return convertInt(o, 0);
	}

	public static int convertInt(Object o, int defValue)
	{
		if (o == null)
		{
			return 0;
		}
		try
		{
			return Integer.parseInt(o.toString());
		} catch (NumberFormatException e)
		{
		}
		return defValue;
	}

	public static String getRemainTime(String s)
	{
		int a = convertInt(s);
		if (a == 0)
		{
			return "立刻";
		}
		int h = (a / 1000) / 60 / 60;
		int m = (a / 1000) / 60 % 60;
		int s1 = ((a / 1000) % 60);
		if (h != 0)
		{
			return h + "小时" + m + "分钟" + s1 + "秒";
		}
		if (m != 0)
		{
			return m + "分钟" + s1 + "秒";
		}
		if (s1 != 0)
		{
			return s1 + "秒";
		}
		return h + "小时" + m + "分钟" + s1 + "秒";
	}

	/**
	 * 根据用户nation属性，转换成中文属性 1，2，3 对应 魏,蜀,吴
	 * 
	 * @param s
	 * @return
	 */
	public static String getNation(String s)
	{
		if (s.equals("1"))
		{
			return "魏国";
		} else if (s.equals("2"))
		{
			return "蜀国";
		} else
		{
			return "吴国";
		}
	}

	/**
	 * 获取单次操作结果
	 * 
	 * @param x
	 * @return
	 */
	public static String getResultState(XmlNodeData x)
	{
		List<XmlNodeData> xList = XmlDataUtil.getNodeInfoList(x, "results.state");
		if (xList.size() > 0)
		{
			return xList.get(0).getNodeValue();
		}
		return "-1";
	}

	/**
	 * 输出s类型，过滤null
	 * 
	 * @param s
	 * @return
	 */
	public static String getStr(Object s)
	{
		if (s == null)
		{
			return "";
		}
		return s.toString();
	}

	/**
	 * 获取单次提示信息
	 * 
	 * @param x
	 * @return
	 */
	public static String getResultMessage(XmlNodeData x)
	{
		String xList = XmlDataUtil.getNodeData(x, "results.message");
		return xList;
	}

	public static String getResultException(XmlNodeData x)
	{
		String xList = XmlDataUtil.getNodeData(x, "results.exception");
		return xList;
	}
}
